<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card>
            <!-- 搜索按钮区域 -->
            <el-row>
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="orderForm.query">
                        <el-button slot="append" icon="el-icon-search" @click="queryOrder"></el-button>
                    </el-input>
                </el-col>
            </el-row>
            <!-- 表格区域 -->
            <el-table :data="orderlist"  border stripe>
                <el-table-column label="#" type="index" ></el-table-column>
                <el-table-column label="订单编号" prop="order_number"></el-table-column>
                <el-table-column label="订单价格" prop="order_price"></el-table-column>
                <el-table-column label="是否付款" prop="order_pay">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.order_price==='1'" type="success" effect="dark" size="mini">已付款</el-tag>
                        <el-tag v-else type="danger" effect="dark" size="mini">未付款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="是否发货" prop="is_send"></el-table-column>
                <el-table-column label="下单时间" prop="create_time">
                    <template slot-scope="scope">
                        {{scope.row.create_time|dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template>
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editOrder"></el-button>
                        <el-button type="success" icon="el-icon-location" size="mini" @click="showProgressBox"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页功能 -->
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="orderForm.pagenum"
                    :page-sizes="[10, 15, 20, 25]"
                    :page-size="orderForm.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
        </el-card>
        <!-- 编辑订单地址 -->
        <el-dialog
            title="修改地址"
            :visible.sync="editOrderDialogVisible"
            width="50%"
            @close="editOrderDialogClosed"
        >
            <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px" class="demo-ruleForm">
                <el-form-item label="省市区/县" prop="address1">
                    <el-cascader :options="cityData" v-model="addressForm.address1" :props="cascaderProp"></el-cascader>
                </el-form-item>
                <el-form-item label="详细地址" prop="address2">
                    <el-input v-model="addressForm.address2"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editOrderDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmOrder">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 展示物流进度的对话框 -->
        <el-dialog
        title="物流进度"
        :visible.sync="progressVisible"
        width="50%"
        >
            <el-timeline>
            <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.time">
            {{activity.context}}
            </el-timeline-item>
            </el-timeline>
        </el-dialog>
    </div>
</template>

<script>
import cityData from './citydata.js'
import kuaidiData from './kuaidi.js'
export default {
    data(){
        return{
            //订单数据列表
            orderlist:[],
            //订单的数据绑定对象也可以用于请求参数的提交
            orderForm:{
                // query: '',
                pagesize: 10,
                pagenum: 1,
                // user_id: '',
                // pay_status: '',
                // is_send: '',    
                // order_fapiao_title: '',
                // order_fapiao_company: '',
                // order_fapiao_content: '',
                // consignee_addr: ''
            },
            //分页数据总条数
            total: 0,
            //修改地址对话框的显示与隐藏
            editOrderDialogVisible: false,
            //修改对话框绑定的表单数据对象
            addressForm:{
                address1:[],
                address2:''
            },
            //表单规则数据对象
            addressFormRules:{
                address1:[{type: 'array',required:true,message:'请选择省市区',tigger:'change'}],
                address2:[{required:true,message:'请输入详细地址',tigger:'blur'}]
            },
            //城市三级联动的数据对象
            cityData,
            //配置选项
            cascaderProp:{
                expandTrigger:'hover',
                value:'value',
                label:'label'
            },
            //物流进度对话框的显示与隐藏
            progressVisible: false,
            //物流进度信息数据
            progressInfo:[]
        }
    },
    created(){
        this.getOrderList()
    },
    methods:{
        //获取订单列表
        async getOrderList(){
            const {data:res}=await this.$http.get('orders',{params:this.orderForm})
            if(res.meta.status!==200) return this.$message.error('获取订单列表失败')
            this.orderlist=res.data.goods
            this.total=res.data.total
        },
        //切换一页有几条数据
        handleSizeChange(newSize){
            this.orderForm.pagesize=newSize
            //请求参数发生变化数据库响应数据发生变化重新渲染列表表格(更新表格数据)
            this.getOrderList()
        },
        //切换第几页
        handleCurrentChange(newPage){
            this.orderForm.pagenum=newPage
            //请求参数发生变化数据库响应数据发生变化重新渲染列表表格(更新表格数据)
            this.getOrderList()
        },
        //查询
        queryOrder(){
            this.getOrderList()
        },
        //编辑用户地址
        editOrder(){
            this.editOrderDialogVisible=true
        },
        //关闭修改地址Dialog对话框一定会触发
        editOrderDialogClosed(){
            this.$refs.addressFormRef.resetFields()
        },
        //修改地址对话框确定
        confirmOrder(){
            this.$refs.addressFormRef.validate(valid=>{
                console.log(valid);
                if(!valid) return
                console.log(this.addressForm.address1);
                this.editOrderDialogVisible = false
            })
        },
        //显示物流进度对话框
        async showProgressBox(){
            this.progressInfo=JSON.parse(kuaidiData)
            console.log(this.progressInfo);
            this.progressVisible=true
        }
    }
}
</script>

<style lang="less" scoped>
    .el-cascader{
        width: 100%;
    }
</style>